package org.gpf.game.tank.online;

import java.awt.Color;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Label;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
/**
 * 坦克的游戏场所
 * @author gaopengfei
 * @date 2015-4-26 下午2:16:08
 */
public class TankClient extends Frame {
	
	private static final long serialVersionUID = 1L;
	public static final int GAME_WIDTH = 800; // 游戏宽高
	public static final int GAME_HEIGHT = 600;
	
	private static final int SCREEN_WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width;
	private static final int SCREEN_HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height;
 
	Tank myTank = new Tank(50, 50, true, Dir.STOP, this); // 新建一个主站坦克

	List<Missile> missiles = new ArrayList<Missile>(); // 存放子弹【子弹可以连发】
	List<Explode> explodes = new ArrayList<Explode>(); // 爆炸的坦克
	List<Tank> tanks = new ArrayList<Tank>();			// 存放坦克【多个坦克】

	Image offScreenImage = null;
	
	NetClient nc = new NetClient(this);	// 网络客户端
	
	ConnDialog dialog = new ConnDialog();

	/**
	 * 重写父类的绘图方法
	 */
	@Override
	public void paint(Graphics g) {
		g.drawString("missiles count:" + missiles.size(), 10, 50); // 游戏数据的显示
		g.drawString("explodes count:" + explodes.size(), 10, 70);
		g.drawString("tanks    count:" + tanks.size(), 10, 90);

		for (int i = 0; i < missiles.size(); i++) {
			Missile m = missiles.get(i);
//			m.hitTanks(tanks);
			if (m.hitTank(myTank)) { // 打中了主战坦克
				Msg msg = new TankDeadMsg(myTank.id); // 坦克死亡消息
				nc.send(msg);
				msg = new MissileDeadMsg(m.tankId,m.id);	// 子弹死亡消息
				nc.send(msg);
			}
			m.draw(g);
		}

		for (int i = 0; i < explodes.size(); i++) {
			Explode e = explodes.get(i);
			e.draw(g);
		}

		for (int i = 0; i < tanks.size(); i++) {
			Tank t = tanks.get(i);
			t.draw(g);
		}

		myTank.draw(g);

	}

	/**
	 * 重写父类的update方法用于实现双缓冲
	 */
	@Override
	public void update(Graphics g) {
		if (offScreenImage == null) {
			offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT); // 创建一幅用于双缓冲的、可在屏幕外绘制的图像。
		}
		Graphics gOffScreen = offScreenImage.getGraphics();
		Color c = gOffScreen.getColor();
		gOffScreen.setColor(Color.GREEN);
		gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
		gOffScreen.setColor(c);
		paint(gOffScreen);
		g.drawImage(offScreenImage, 0, 0, null);
	}

	/**
	 * 显示游戏界面
	 */
	public void launchFrame() {

		this.setSize(GAME_WIDTH, GAME_HEIGHT);
		this.setLocation((SCREEN_WIDTH - GAME_WIDTH) / 2, (SCREEN_HEIGHT - GAME_HEIGHT) / 2);
		this.setTitle("坦克大战-网络版");
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		this.setResizable(false);
		this.setBackground(Color.GREEN);
		this.addKeyListener(new KeyMonitor());
		this.setVisible(true);

		new Thread(new PaintThread()).start();
		
//		nc.connect("127.0.0.1", TankServer.TCP_PORT);// 连接服务器
	}

	public static void main(String[] args) {
		TankClient tc = new TankClient();
		tc.launchFrame();
	}

	/**
	 * 该线程用于界面的重绘，每50ms执行一次
	 * @author gaopengfei
	 * @date 2015-4-21 下午5:28:49
	 */
	class PaintThread implements Runnable {

		public void run() {
			while (true) {
				repaint();
				try {
					Thread.sleep(50);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}

	}

	class KeyMonitor extends KeyAdapter {

		@Override
		public void keyReleased(KeyEvent e) {
			myTank.keyReleased(e);
		}

		@Override
		public void keyPressed(KeyEvent e) {
			int key = e.getKeyCode();
			if (key==KeyEvent.VK_C) {
				dialog.setVisible(true);
			}else {
				myTank.keyPressed(e);
			}
		}

	}

	class ConnDialog extends Dialog{
		
		private static final long serialVersionUID = 1L;
		JButton button = new JButton("确定");
		TextField tfIP = new TextField("127.0.0.1",12);
		TextField tfPort = new TextField("" + TankServer.TCP_PORT,4);
		TextField tfMyUDPPort = new TextField("2222",4);
		public ConnDialog() {
			super(TankClient.this, true);
			
			this.setLayout(new FlowLayout());
			
			this.add(new Label("IP:"));
			this.add(tfIP);
			this.add(new Label("Port:"));
			this.add(tfPort);
			this.add(new Label("My UDP Port:"));
			this.add(tfMyUDPPort);
			this.add(button);
			this.setLocation(300, 300);
			this.pack();	//自动适应大小
			button.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					
					String IP = tfIP.getText().trim();
					int port = Integer.parseInt(tfPort.getText().trim());
					int myUDPPort = Integer.parseInt(tfMyUDPPort.getText().trim());
					nc.setUdpPort(myUDPPort);
					nc.connect(IP, port);
					setVisible(false);
					
				}
			});
			this.addWindowListener(new WindowAdapter() {

				@Override
				public void windowClosing(WindowEvent e) {
					setVisible(false);
				}
				
			});
		}
		
		
	}
}
